package easy;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2019/2/26 13:50
 */
public class PalindromeNumber {
    public static void main(String[] args) {
        Solution2 solution = new PalindromeNumber().new Solution2();
        int a = 1221;
        System.out.println(solution.isPalindrome(a));
    }

    private class Solution {
        public boolean isPalindrome(int x) {
            if (x < 0) {
                return false;
            }
            char[] chars = String.valueOf(x).toCharArray();
            for (int i = 0; i < chars.length / 2; i++) {
                if (chars[i] != chars[chars.length - 1 - i]) {
                    return false;
                }
            }
            return true;
        }
    }

    private class Solution2 {
        public boolean isPalindrome(int x) {
            if (x < 0) {
                return false;
            }
            char[] chars = String.valueOf(x).toCharArray();
            int half = chars.length / 2;
            int low = (int) (x % (Math.pow(10, half)));
            int high = (int) (chars.length % 2 == 1 ? (x / Math.pow(10, half + 1)) : (x / Math.pow(10, half)));

            return low == reverseInt(high);
        }

        private int reverseInt(int m) {
            String str = String.valueOf(m);
            char[] chars = str.toCharArray();
            for (int i = 0; i < chars.length / 2; i++) {
                int index = chars.length - 1 - i;
                char temp = chars[i];
                chars[i] = chars[index];
                chars[index] = temp;
            }
            String temp = new String(chars);
            return Integer.parseInt(temp);
        }
    }
}
